<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\service\v1\OrderNodeService;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1OrderNode;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 订单节点退款
 */
class V1OrderNodeRefundController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需鉴权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];
    
    /**
     * @var V1OrderNode
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1OrderNode;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        $user_id = $request->input('user_id');
        return view('v1-order-node-refund/index', compact('user_id'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $where['deleted'] = CommonConstant::DELETE_0;
        $where['status'] = CommonConstant::DEFAULT_0;
        if(!isset($where['refund_status'])){
            $where['refund_status'] = ['in', [CommonConstant::DEFAULT_1, CommonConstant::DEFAULT_2, CommonConstant::DEFAULT_3]];
        }
        $query = $this->doSelect($where, $field, $order);
        $query->with([
            'server' => function ($query) {
                $query->where('deleted', CommonConstant::DELETE_0)
                    ->select(['id','multi_port_status','multi_port']);
            }
        ]);
        $paginator = $query->paginate($limit);
        $total = $paginator->total();
        $items = $paginator->items();
        if ($items) {
            foreach ($items as $item) {
                Util::getSk5Port($item);
            }
        }
        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-order-node-expire/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-order-node-expire/update');
    }


    /**
     * 退款审核
     * @param Request $request
     * @return Response
     * @throws BusinessException
     **/
    public function audit(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $ids = $request->input('ids');
            $ids = is_array($ids) ? $ids : explode(',', $ids);
            $status = (integer)$request->post('status', 0);
            $audit_desc = $request->post('audit_desc', '');
            if (!$ids) {
                return $this->json(1, '节点参数错误！');
            }
            if (!in_array($status,[CommonConstant::DEFAULT_2,CommonConstant::DEFAULT_3])) {
                return $this->json(1, '审核状态错误！');
            }
            if (!$audit_desc) {
                return $this->json(1, '请输入审核备注！');
            }
            $params = compact('ids','status','audit_desc');
            $admin = admin();
            if ($admin['user_status'] != CommonConstant::DEFAULT_1) {
                return $this->json(1, '用户不是管理员！');
            }

            $result = OrderNodeService::auditRefund($params,$admin);
            return $this->json(0, $status == CommonConstant::DEFAULT_2 ? '退款成功':'退款驳回',$result);
        }
    }

}
